package cc.kzc.adminauth.misc;

import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class DateTime {

	public static final String DATE_TIME_MILLI_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";

	public static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";

	public static final String DATE_FORMAT = "yyyy-MM-dd";

	public static final String TIME_FORMAT = "HH:mm:ss";

	public static final String TIME_ZONE = "+8";

	public static final String DATETIME_START_STRING = "2000-01-01T00:00:00";

	public static final String DATETIME_END_STRING = "9999-12-31T23:59:59";

	public static final String DATE_START_STRING = "2000-01-01";

	public static final String DATE_END_STRING = "9999-12-31";

	public static String nowDateTimeMillis() {
		return LocalDateTime.now().format(DateTimeFormatter.ofPattern(DATE_TIME_MILLI_FORMAT));
	}

	public static String dateTimeMillis(long milli) {
		LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(milli), ZoneId.systemDefault());
		return ldt.format(DateTimeFormatter.ofPattern(DATE_TIME_MILLI_FORMAT));
	}

	public static String nowDateTime() {
		return LocalDateTime.now().format(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT));
	}

	public static String nowDate() {
		return LocalDate.now().format(DateTimeFormatter.ofPattern(DATE_FORMAT));
	}

	public static String yesterday() {
		return LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern(DATE_FORMAT));
	}

	public static String monday() {
		return LocalDate.now().with(DayOfWeek.MONDAY).format(DateTimeFormatter.ofPattern(DATE_FORMAT));
	}

	public static String sunday() {
		return LocalDate.now().with(DayOfWeek.SUNDAY).format(DateTimeFormatter.ofPattern(DATE_FORMAT));
	}

	public static String nextWeekMonday() {
		return LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.MONDAY))
				.format(DateTimeFormatter.ofPattern(DATE_FORMAT));
	}

	public static long currentTimeMillis() {
		return System.currentTimeMillis();
	}

	public static long toMilliSecond(String dateTime) {
		if (null == dateTime) {
			dateTime = DATETIME_START_STRING;
		}
		LocalDateTime ldt = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(DATE_TIME_FORMAT));
		return ldt.toInstant(ZoneOffset.of(TIME_ZONE)).toEpochMilli();

	}

	public static String currentTimeMillis(int radix) {
		if (radix < Character.MIN_RADIX) {
			radix = Character.MIN_RADIX;
		} else if (radix > Character.MAX_RADIX) {
			radix = Character.MAX_RADIX;
		}
		String str = Long.toString(System.currentTimeMillis(), radix);
		return str;
	}

}
